package com.example.mywiseelves.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mywiseelves.R;
import com.example.mywiseelves.bean.ResourcesBean;
import com.example.mywiseelves.dao.ResourcesDao;
import com.example.mywiseelves.view.dialog.MyDialog;

import java.util.List;

/**
 * @author ZJK
 * @data 2021 18
 * @description
 */
public class SplashAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<ResourcesBean> resourcesBeanList;
    //资源dao对象
    ResourcesDao resourcesDao;
    private MyDialog mySplashDialog;
    View view;
    LocalBroadcastManager localBroadcastManager;
    public SplashAdapter(Context context, List<ResourcesBean> resourcesBeanList) {
        resourcesDao = new ResourcesDao(context);
        this.context = context;
        this.resourcesBeanList = resourcesBeanList;
        localBroadcastManager=LocalBroadcastManager.getInstance(context);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView SplashImageIv;
        TextView SplashStatesTv;

        public ViewHolder(View view) {
            super(view);
            SplashImageIv = view.findViewById(R.id.splash_image_iv);
            SplashStatesTv = view.findViewById(R.id.splash_states_tv);
        }
    }

    public void refreshListData(List<ResourcesBean> resourcesBeanList) {
        this.resourcesBeanList = resourcesBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_splash_recyclerview, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ResourcesBean resourcesBean = resourcesBeanList.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        initOnclickListener(viewHolder, resourcesBean);
        //清空：
        viewHolder.SplashImageIv.setImageResource(0);
        viewHolder.SplashStatesTv.setText("");

        viewHolder.SplashImageIv.setImageResource(resourcesBean.getSplash_image());
        if (resourcesBean.getSplash_states() == 1) {
            viewHolder.SplashStatesTv.setText("已启用");
        }

    }

    @Override
    public int getItemCount() {
        return resourcesBeanList.size();
    }

    /**
     * 初始化点击监听
     */
    public void initOnclickListener(ViewHolder viewHolder, ResourcesBean resourcesBean) {
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (resourcesBean.getSplash_states() == 0) {
                    openSplashDialog(resourcesBean);
                }else {
                    Toast.makeText(context, "您当前已经是这张图片了", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    /**
     * 点击开启切换启动页图片弹窗
     */
    public void openSplashDialog(ResourcesBean resourcesBean) {
        mySplashDialog = new MyDialog(context, R.style.ExitDialog);
        mySplashDialog.setTitle("更换启动页");
        //设置内容
        mySplashDialog.setMessage("您确定要更换这张图片，作为启动页面吗？");
        mySplashDialog.setYesOnclickListener("是的", new MyDialog.onYesOnclickListener() {
            @Override
            public void onYesOnclick() {
                updateSplashStates(resourcesBean);
                mySplashDialog.dismiss();
            }
        });
        mySplashDialog.setNoOnclickListener("不，等会", new MyDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                mySplashDialog.dismiss();
            }
        });
        mySplashDialog.show();
    }

    /**
     * 检查是否修改修改精灵解锁状态,修改用户点数
     */
    public void updateSplashStates(ResourcesBean resourcesBean) {
        resourcesDao.allSplashStatesUpdate();
        resourcesDao.splashStatesUpdate(resourcesBean);
        Intent intent = new Intent("updateSplash");
        localBroadcastManager.sendBroadcast(intent);
        Toast.makeText(context, "切换成功，可退出重进，查看效果", Toast.LENGTH_LONG).show();
    }
}
